home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / enscript.4 / enscript / enscript-1.4.0 / intl / dcgettext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-13  |  15.7 KB  |  594 lines

  1. /* dcgettext.c -- implementation of the dcgettext(3) function
  2.    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21.  
  22. #include <sys/types.h>
  23.  
  24. #ifdef __GNUC__
  25. # define alloca __builtin_alloca
  26. # define HAVE_ALLOCA 1
  27. #else
  28. # if defined HAVE_ALLOCA_H || defined _LIBC
  29. #  include <alloca.h>
  30. # else
  31. #  ifdef _AIX
  32.  #pragma alloca
  33. #  else
  34. #   ifndef alloca
  35. char *alloca ();
  36. #   endif
  37. #  endif
  38. # endif
  39. #endif
  40.  
  41. #include <errno.h>
  42. #ifndef errno
  43. extern int errno;
  44. #endif
  45.  
  46. #if defined STDC_HEADERS || defined _LIBC
  47. # include <stdlib.h>
  48. #else
  49. char *getenv ();
  50. # ifdef HAVE_MALLOC_H
  51. #  include <malloc.h>
  52. # else
  53. void free ();
  54. # endif
  55. #endif
  56.  
  57. #if defined HAVE_STRING_H || defined _LIBC
  58. # include <string.h>
  59. #else
  60. # include <strings.h>
  61. #endif
  62. #if !HAVE_STRCHR && !defined _LIBC
  63. # ifndef strchr
  64. #  define strchr index
  65. # endif
  66. #endif
  67.  
  68. #if defined HAVE_UNISTD_H || defined _LIBC
  69. # include <unistd.h>
  70. #endif
  71.  
  72. #include "gettext.h"
  73. #include "gettextP.h"
  74. #ifdef _LIBC
  75. # include <libintl.h>
  76. #else
  77. # include "libgettext.h"
  78. #endif
  79. #include "hash-string.h"
  80.  
  81. /* @@ end of prolog @@ */
  82.  
  83. #ifdef _LIBC
  84. /* Rename the non ANSI C functions.  This is required by the standard
  85.    because some ANSI C functions will require linking with this object
  86.    file and the name space must not be polluted.  */
  87. # define getcwd __getcwd
  88. # define stpcpy __stpcpy
  89. #else
  90. # if !defined HAVE_GETCWD
  91. char *getwd ();
  92. #  define getcwd(buf, max) getwd (buf)
  93. # else
  94. char *getcwd ();
  95. # endif
  96. # ifndef HAVE_STPCPY
  97. static char *stpcpy PARAMS ((char *dest, const char *src));
  98. # endif
  99. #endif
  100.  
  101. /* Amount to increase buffer size by in each try.  */
  102. #define PATH_INCR 32
  103.  
  104. /* The following is from pathmax.h.  */
  105. /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
  106.    PATH_MAX but might cause redefinition warnings when sys/param.h is
  107.    later included (as on MORE/BSD 4.3).  */
  108. #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
  109. # include <limits.h>
  110. #endif
  111.  
  112. #ifndef _POSIX_PATH_MAX
  113. # define _POSIX_PATH_MAX 255
  114. #endif
  115.  
  116. #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
  117. # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
  118. #endif
  119.  
  120. /* Don't include sys/param.h if it already has been.  */
  121. #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
  122. # include <sys/param.h>
  123. #endif
  124.  
  125. #if !defined(PATH_MAX) && defined(MAXPATHLEN)
  126. # define PATH_MAX MAXPATHLEN
  127. #endif
  128.  
  129. #ifndef PATH_MAX
  130. # define PATH_MAX _POSIX_PATH_MAX
  131. #endif
  132.  
  133. /* XPG3 defines the result of `setlocale (category, NULL)' as:
  134.    ``Directs `setlocale()' to query `category' and return the current
  135.      setting of `local'.''
  136.    However it does not specify the exact format.  And even worse: POSIX
  137.    defines this not at all.  So we can use this feature only on selected
  138.    system (e.g. those using GNU C Library).  */
  139. #ifdef _LIBC
  140. # define HAVE_LOCALE_NULL
  141. #endif
  142.  
  143. /* Name of the default domain used for gettext(3) prior any call to
  144.    textdomain(3).  The default value for this is "messages".  */
  145. const char _nl_default_default_domain[] = "messages";
  146.  
  147. /* Value used as the default domain for gettext(3).  */
  148. const char *_nl_current_default_domain = _nl_default_default_domain;
  149.  
  150. /* Contains the default location of the message catalogs.  */
  151. const char _nl_default_dirname[] = GNULOCALEDIR;
  152.  
  153. /* List with bindings of specific domains created by bindtextdomain()
  154.    calls.  */
  155. struct binding *_nl_domain_bindings;
  156.  
  157. /* Prototypes for local functions.  */
  158. static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
  159.                    const char *msgid));
  160. static const char *category_to_name PARAMS ((int category));
  161. static const char *guess_category_value PARAMS ((int category,
  162.                          const char *categoryname));
  163.  
  164.  
  165. /* For those loosing systems which don't have `alloca' we have to add
  166.    some additional code emulating it.  */
  167. #ifdef HAVE_ALLOCA
  168. /* Nothing has to be done.  */
  169. # define ADD_BLOCK(list, address) /* nothing */
  170. # define FREE_BLOCKS(list) /* nothing */
  171. #else
  172. struct block_list
  173. {
  174.   void *address;
  175.   struct block_list *next;
  176. };
  177. # define ADD_BLOCK(list, addr)                              \
  178.   do {                                          \
  179.     struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
  180.     /* If we cannot get a free block we cannot add the new element to          \
  181.        the list.  */                                  \
  182.     if (newp != NULL) {                                  \
  183.       newp->address = (addr);                              \
  184.       newp->next = (list);                              \
  185.       (list) = newp;                                  \
  186.     }                                          \
  187.   } while (0)
  188. # define FREE_BLOCKS(list)                              \
  189.   do {                                          \
  190.     while (list != NULL) {                              \
  191.       struct block_list *old = list;                          \
  192.       list = list->next;                              \
  193.       free (old);                                  \
  194.     }                                          \
  195.   } while (0)
  196. # undef alloca
  197. # define alloca(size) (malloc (size))
  198. #endif    /* have alloca */
  199.  
  200.  
  201. /* Names for the libintl functions are a problem.  They must not clash
  202.    with existing names and they should follow ANSI C.  But this source
  203.    code is also used in GNU C Library where the names have a __
  204.    prefix.  So we have to make a difference here.  */
  205. #ifdef _LIBC
  206. # define DCGETTEXT __dcgettext
  207. #else
  208. # define DCGETTEXT dcgettext__
  209. #endif
  210.  
  211. /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
  212.    locale.  */
  213. char *
  214. DCGETTEXT (domainname, msgid, category)
  215.      const char *domainname;
  216.      const char *msgid;
  217.      int category;
  218. {
  219. #ifndef HAVE_ALLOCA
  220.   struct block_list *alloca_list = NULL;
  221. #endif
  222.   struct loaded_l10nfile *domain;
  223.   struct binding *binding;
  224.   const char *categoryname;
  225.   const char *categoryvalue;
  226.   char *dirname, *xdomainname;
  227.   char *single_locale;
  228.   char *retval;
  229.   int saved_errno = errno;
  230.  
  231.   /* If no real MSGID is given return NULL.  */
  232.   if (msgid == NULL)
  233.     return NULL;
  234.  
  235.   /* If DOMAINNAME is NULL, we are interested in the default domain.  If
  236.      CATEGORY is not LC_MESSAGES this might not make much sense but the
  237.      defintion left this undefined.  */
  238.   if (domainname == NULL)
  239.     domainname = _nl_current_default_domain;
  240.  
  241.   /* First find matching binding.  */
  242.   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  243.     {
  244.       int compare = strcmp (domainname, binding->domainname);
  245.       if (compare == 0)
  246.     /* We found it!  */
  247.     break;
  248.       if (compare < 0)
  249.     {
  250.       /* It is not in the list.  */
  251.       binding = NULL;
  252.       break;
  253.     }
  254.     }
  255.  
  256.   if (binding == NULL)
  257.     dirname = (char *) _nl_default_dirname;
  258.   else if (binding->dirname[0] == '/')
  259.     dirname = binding->dirname;
  260.   else
  261.     {
  262.       /* We have a relative path.  Make it absolute now.  */
  263.       size_t dirname_len = strlen (binding->dirname) + 1;
  264.       size_t path_max;
  265.       char *ret;
  266.  
  267.       path_max = (unsigned) PATH_MAX;
  268.       path_max += 2;        /* The getcwd docs say to do this.  */
  269.  
  270.       dirname = (char *) alloca (path_max + dirname_len);
  271.       ADD_BLOCK (alloca_list, dirname);
  272.  
  273.       errno = 0;
  274.       while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
  275.     {
  276.       path_max += PATH_INCR;
  277.       dirname = (char *) alloca (path_max + dirname_len);
  278.       ADD_BLOCK (alloca_list, dirname);
  279.       errno = 0;
  280.     }
  281.  
  282.       if (ret == NULL)
  283.     {
  284.       /* We cannot get the current working directory.  Don't signal an
  285.          error but simply return the default string.  */
  286.       FREE_BLOCKS (alloca_list);
  287.       errno = saved_errno;
  288.       return (char *) msgid;
  289.     }
  290.  
  291.       /* We don't want libintl.a to depend on any other library.  So
  292.      we avoid the non-standard function stpcpy.  In GNU C Library
  293.      this function is available, though.  Also allow the symbol
  294.      HAVE_STPCPY to be defined.  */
  295.       stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
  296.     }
  297.  
  298.   /* Now determine the symbolic name of CATEGORY and its value.  */
  299.   categoryname = category_to_name (category);
  300.   categoryvalue = guess_category_value (category, categoryname);
  301.  
  302.   xdomainname = (char *) alloca (strlen (categoryname)
  303.                  + strlen (domainname) + 5);
  304.   ADD_BLOCK (alloca_list, xdomainname);
  305.   /* We don't want libintl.a to depend on any other library.  So we
  306.      avoid the non-standard function stpcpy.  In GNU C Library this
  307.      function is available, though.  Also allow the symbol HAVE_STPCPY
  308.      to be defined.  */
  309.   stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  310.           domainname),
  311.       ".mo");
  312.  
  313.   /* Creating working area.  */
  314.   single_locale = (char *) alloca (strlen (categoryvalue) + 1);
  315.   ADD_BLOCK (alloca_list, single_locale);
  316.  
  317.  
  318.   /* Search for the given string.  This is a loop because we perhaps
  319.      got an ordered list of languages to consider for th translation.  */
  320.   while (1)
  321.     {
  322.       /* Make CATEGORYVALUE point to the next element of the list.  */
  323.       while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  324.     ++categoryvalue;
  325.       if (categoryvalue[0] == '\0')
  326.     {
  327.       /* The whole contents of CATEGORYVALUE has been searched but
  328.          no valid entry has been found.  We solve this situation
  329.          by implicitely appending a "C" entry, i.e. no translation
  330.          will take place.  */
  331.       single_locale[0] = 'C';
  332.       single_locale[1] = '\0';
  333.     }
  334.       else
  335.     {
  336.       char *cp = single_locale;
  337.       while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  338.         *cp++ = *categoryvalue++;
  339.       *cp = '\0';
  340.     }
  341.  
  342.       /* If the current locale value is C (or POSIX) we don't load a
  343.      domain.  Return the MSGID.  */
  344.       if (strcmp (single_locale, "C") == 0
  345.       || strcmp (single_locale, "POSIX") == 0)
  346.     {
  347.       FREE_BLOCKS (alloca_list);
  348.       errno = saved_errno;
  349.       return (char *) msgid;
  350.     }
  351.  
  352.  
  353.       /* Find structure describing the message catalog matching the
  354.      DOMAINNAME and CATEGORY.  */
  355.       domain = _nl_find_domain (dirname, single_locale, xdomainname);
  356.  
  357.       if (domain != NULL)
  358.     {
  359.       retval = find_msg (domain, msgid);
  360.  
  361.       if (retval == NULL)
  362.         {
  363.           int cnt;
  364.  
  365.           for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  366.         {
  367.           retval = find_msg (domain->successor[cnt], msgid);
  368.  
  369.           if (retval != NULL)
  370.             break;
  371.         }
  372.         }
  373.  
  374.       if (retval != NULL)
  375.         {
  376.           FREE_BLOCKS (alloca_list);
  377.           errno = saved_errno;
  378.           return retval;
  379.         }
  380.     }
  381.     }
  382.   /* NOTREACHED */
  383. }
  384.  
  385. #ifdef _LIBC
  386. /* Alias for function name in GNU C Library.  */
  387. weak_alias (__dcgettext, dcgettext);
  388. #endif
  389.  
  390.  
  391. static char *
  392. find_msg (domain_file, msgid)
  393.      struct loaded_l10nfile *domain_file;
  394.      const char *msgid;
  395. {
  396.   size_t top, act, bottom;
  397.   struct loaded_domain *domain;
  398.  
  399.   if (domain_file->decided == 0)
  400.     _nl_load_domain (domain_file);
  401.  
  402.   if (domain_file->data == NULL)
  403.     return NULL;
  404.  
  405.   domain = (struct loaded_domain *) domain_file->data;
  406.  
  407.   /* Locate the MSGID and its translation.  */
  408.   if (domain->hash_size > 2 && domain->hash_tab != NULL)
  409.     {
  410.       /* Use the hashing table.  */
  411.       nls_uint32 len = strlen (msgid);
  412.       nls_uint32 hash_val = hash_string (msgid);
  413.       nls_uint32 idx = hash_val % domain->hash_size;
  414.       nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  415.       nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
  416.  
  417.       if (nstr == 0)
  418.     /* Hash table entry is empty.  */
  419.     return NULL;
  420.  
  421.       if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
  422.       && strcmp (msgid,
  423.              domain->data + W (domain->must_swap,
  424.                        domain->orig_tab[nstr - 1].offset)) == 0)
  425.     return (char *) domain->data + W (domain->must_swap,
  426.                       domain->trans_tab[nstr - 1].offset);
  427.  
  428.       while (1)
  429.     {
  430.       if (idx >= domain->hash_size - incr)
  431.         idx -= domain->hash_size - incr;
  432.       else
  433.         idx += incr;
  434.  
  435.       nstr = W (domain->must_swap, domain->hash_tab[idx]);
  436.       if (nstr == 0)
  437.         /* Hash table entry is empty.  */
  438.         return NULL;
  439.  
  440.       if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
  441.           && strcmp (msgid,
  442.              domain->data + W (domain->must_swap,
  443.                        domain->orig_tab[nstr - 1].offset))
  444.              == 0)
  445.         return (char *) domain->data
  446.           + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
  447.     }
  448.       /* NOTREACHED */
  449.     }
  450.  
  451.   /* Now we try the default method:  binary search in the sorted
  452.      array of messages.  */
  453.   bottom = 0;
  454.   top = domain->nstrings;
  455.   while (bottom < top)
  456.     {
  457.       int cmp_val;
  458.  
  459.       act = (bottom + top) / 2;
  460.       cmp_val = strcmp (msgid, domain->data
  461.                    + W (domain->must_swap,
  462.                     domain->orig_tab[act].offset));
  463.       if (cmp_val < 0)
  464.     top = act;
  465.       else if (cmp_val > 0)
  466.     bottom = act + 1;
  467.       else
  468.     break;
  469.     }
  470.  
  471.   /* If an translation is found return this.  */
  472.   return bottom >= top ? NULL : (char *) domain->data
  473.                                 + W (domain->must_swap,
  474.                      domain->trans_tab[act].offset);
  475. }
  476.  
  477.  
  478. /* Return string representation of locale CATEGORY.  */
  479. static const char *
  480. category_to_name (category)
  481.      int category;
  482. {
  483.   const char *retval;
  484.  
  485.   switch (category)
  486.   {
  487. #ifdef LC_COLLATE
  488.   case LC_COLLATE:
  489.     retval = "LC_COLLATE";
  490.     break;
  491. #endif
  492. #ifdef LC_CTYPE
  493.   case LC_CTYPE:
  494.     retval = "LC_CTYPE";
  495.     break;
  496. #endif
  497. #ifdef LC_MONETARY
  498.   case LC_MONETARY:
  499.     retval = "LC_MONETARY";
  500.     break;
  501. #endif
  502. #ifdef LC_NUMERIC
  503.   case LC_NUMERIC:
  504.     retval = "LC_NUMERIC";
  505.     break;
  506. #endif
  507. #ifdef LC_TIME
  508.   case LC_TIME:
  509.     retval = "LC_TIME";
  510.     break;
  511. #endif
  512. #ifdef LC_MESSAGES
  513.   case LC_MESSAGES:
  514.     retval = "LC_MESSAGES";
  515.     break;
  516. #endif
  517. #ifdef LC_RESPONSE
  518.   case LC_RESPONSE:
  519.     retval = "LC_RESPONSE";
  520.     break;
  521. #endif
  522. #ifdef LC_ALL
  523.   case LC_ALL:
  524.     /* This might not make sense but is perhaps better than any other
  525.        value.  */
  526.     retval = "LC_ALL";
  527.     break;
  528. #endif
  529.   default:
  530.     /* If you have a better idea for a default value let me know.  */
  531.     retval = "LC_XXX";
  532.   }
  533.  
  534.   return retval;
  535. }
  536.  
  537. /* Guess value of current locale from value of the environment variables.  */
  538. static const char *guess_category_value (category, categoryname)
  539.      int category;
  540.      const char *categoryname;
  541. {
  542.   const char *retval;
  543.  
  544.   /* The highest priority value is the `LANGUAGE' environment
  545.      variable.  This is a GNU extension.  */
  546.   retval = getenv ("LANGUAGE");
  547.   if (retval != NULL && retval[0] != '\0')
  548.     return retval;
  549.  
  550.   /* `LANGUAGE' is not set.  So we have to proceed with the POSIX
  551.      methods of looking to `LC_ALL', `LC_xxx', and `LANG'.  On some
  552.      systems this can be done by the `setlocale' function itself.  */
  553. #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
  554.   return setlocale (category, NULL);
  555. #else
  556.   /* Setting of LC_ALL overwrites all other.  */
  557.   retval = getenv ("LC_ALL");
  558.   if (retval != NULL && retval[0] != '\0')
  559.     return retval;
  560.  
  561.   /* Next comes the name of the desired category.  */
  562.   retval = getenv (categoryname);
  563.   if (retval != NULL && retval[0] != '\0')
  564.     return retval;
  565.  
  566.   /* Last possibility is the LANG environment variable.  */
  567.   retval = getenv ("LANG");
  568.   if (retval != NULL && retval[0] != '\0')
  569.     return retval;
  570.  
  571.   /* We use C as the default domain.  POSIX says this is implementation
  572.      defined.  */
  573.   return "C";
  574. #endif
  575. }
  576.  
  577. /* @@ begin of epilog @@ */
  578.  
  579. /* We don't want libintl.a to depend on any other library.  So we
  580.    avoid the non-standard function stpcpy.  In GNU C Library this
  581.    function is available, though.  Also allow the symbol HAVE_STPCPY
  582.    to be defined.  */
  583. #if !_LIBC && !HAVE_STPCPY
  584. static char *
  585. stpcpy (dest, src)
  586.      char *dest;
  587.      const char *src;
  588. {
  589.   while ((*dest++ = *src++) != '\0')
  590.     /* Do nothing. */ ;
  591.   return dest - 1;
  592. }
  593. #endif
  594.